import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;





public class ParserWithDocsId extends Parser{
	HashMap<String, Integer> mPath_to_doc_id_map = new HashMap<String, Integer>();
	HashMap<Integer, Integer> mDoc_id_to_class_id = new HashMap<Integer, Integer>();
	HashMap<Integer, DocsContent> mDoc_id_to_content = new HashMap<Integer, DocsContent>();
	String mDocIdFilePath;
	public ParserWithDocsId(String mainFolder, String doc_id_file, String output_doc_file_name) throws IOException {
		super(mainFolder);
		mDocIdFilePath = doc_id_file;
		super.FILE_NAME = output_doc_file_name;
		readDocsFile(mDocIdFilePath);
		
	}
	
	static Integer counter = 0;
	private void readDocsFile(String fileName) throws IOException {
		String line;
		File docsFile = new File(fileName);
		FileInputStream fileInputStream = new FileInputStream(docsFile);
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(fileInputStream));

		// read the file line by line
		while ((line = bufferedReader.readLine()) != null) {
			
			String[] parts = line.split(" ", 3);
			Integer doc_id = Integer.parseInt(parts[0].trim());
			String doc_path_code = parts[1].trim();
			doc_path_code = doc_path_code.replace(":", "_" );
			if(doc_path_code.endsWith("."))
			{
				doc_path_code = doc_path_code.substring(0,doc_path_code.length()-1);
			}
			
			Integer class_gt_id = Integer.parseInt(parts[2].trim());
			mPath_to_doc_id_map.put(doc_path_code, doc_id);
			mDoc_id_to_class_id.put(doc_id, class_gt_id);
		}
		
		
		bufferedReader.close();
	}
	@Override
	protected void AddFileToDb(DocsContent doc_content, String doc_path)
	{
		
		int start_position_for_code = doc_path.indexOf("webkb");
		String extracted_path_code =  doc_path.substring(start_position_for_code);
		//extracted_path_code = extracted_path_code.replace('/', '^');
		//extracted_path_code = extracted_path_code.replace("_", ":");
		
		extracted_path_code = extracted_path_code.replace('\\', '/');
		if(extracted_path_code.endsWith("_") )
		{
			extracted_path_code = extracted_path_code.substring(0,extracted_path_code.length()-1);
		}
		
		System.out.println("i = " + counter.toString());
		Integer doc_id = mPath_to_doc_id_map.get(extracted_path_code);
		Integer cluster_id = mDoc_id_to_class_id.get(doc_id);		
		mDoc_id_to_content.put(doc_id, doc_content);
		DocsContent doc_with_id = new DocsContent(doc_content);
		doc_with_id.mData = doc_id.toString() + "   "+ cluster_id.toString() +"   "+ doc_content.mData; 
		super.AddFileToDb(doc_with_id, doc_path);
		++counter;
	
	}
	

}